package com.lanou.tk.tourism.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.tk.tourism.R;
import com.lanou.tk.tourism.entity.RecommendData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/24.
 */
public class RecommendRvAdapter extends RecyclerView.Adapter<RecommendRvAdapter.LatestViewHolder> {


    private List<RecommendData> recommendDatas;//存放实体类的集合
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;

    public RecommendRvAdapter(Context c) {
        queue = Volley.newRequestQueue(c);

    }


    public void addData(List<RecommendData> datas) {
        recommendDatas = datas;
        notifyDataSetChanged();
    }


    //组件缓存内部类
    class LatestViewHolder extends RecyclerView.ViewHolder {
        private ImageView photo;//最左侧的图片
        private TextView title;//上面标题
        private TextView username;//作者
        private TextView views;//观看数量
        private TextView replys;//阅读数量

        public LatestViewHolder(View v) {

            super(v);
            //初始化组件
            photo = (ImageView) v.findViewById(R.id.recommend_Rv_image);
            title = (TextView) v.findViewById(R.id.recommend_Rv_title);
            username = (TextView) v.findViewById(R.id.recommend_Rv_username);
            views = (TextView) v.findViewById(R.id.recommend_Rv_Views);
            replys = (TextView) v.findViewById(R.id.recommend_Rv_repyls);

        }
    }


    @Override
    public int getItemCount() {
        return recommendDatas != null && recommendDatas.size() > 0 ? recommendDatas.size() : 0;
    }


    @Override
    public void onBindViewHolder(final LatestViewHolder holder, int position) {
        if (recommendDatas != null && recommendDatas.size() > 0) {
            RecommendData data = recommendDatas.get(position);
            holder.title.setText(data.getTitle()); //设置上面title
            holder.username.setText(data.getUsername());//设置作者
          holder.views.setText(data.getViews()+"");//设置浏览数量
            holder.replys.setText(data.getReplys());//设置阅读数量

            //在进行图片请求前先判断返回的集合中是否有图片
//            if(data.getPhoto() != null ){
            String imgUrl = data.getPhoto();
            //网络拉取图片并显示
            getBitmap(imgUrl, holder.photo);

        }
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }

    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }


//    }

    public void getBitmap(String url, final ImageView imageView) {
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {

            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);

            }
        }, 300, 300, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);//将本次图片请求加入到队列中

    }

    @Override
    public LatestViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {

        //获得布局加载器
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        View v = inflater.inflate(R.layout.fragment_recommend_recycler_view, null);


        return new LatestViewHolder(v);
    }


}
